package com.example.smartcitymovie.ui.selfcenter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.R;
import com.example.smartcitymovie.bean.ChangeUserMessageBean;
import com.example.smartcitymovie.bean.ResultBean;
import com.example.smartcitymovie.dao.AppDatabase;
import com.example.smartcitymovie.dao.Userdao;
import com.example.smartcitymovie.databinding.ActivityUserMessageBinding;
import com.example.smartcitymovie.entity.User;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class UserMessageActivity extends AppCompatActivity {

    ActivityUserMessageBinding binding;
    Userdao userdao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityUserMessageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        AppDatabase db = Room.databaseBuilder(UserMessageActivity.this,
                AppDatabase.class, "database").build();
        userdao = db.userDao();
        getSupportActionBar().setTitle("个人信息");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initView();
    }

    private void initView() {
        binding.TIETNickName.setText(MyApplication.getSharedPreferences().getString("nickName", ""));
        binding.RBMan.setChecked(MyApplication.getSharedPreferences().getString("sex", "0").equals("0"));
        binding.RBWoman.setChecked(MyApplication.getSharedPreferences().getString("sex", "0").equals("1"));
        binding.TIETTel.setText(MyApplication.getSharedPreferences().getString("phonenumber", ""));
        binding.TIETIDCard.setText(MyApplication.getSharedPreferences().getString("idCard", ""));
        Glide.with(binding.IVUserAvatar).load(MyApplication.getSharedPreferences().getString("avatar", "")).transform(new CircleCrop()).placeholder(R.drawable.ic_baseline_account_circle_24).into(binding.IVUserAvatar);
        binding.btnSaveMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ChangeUserMessageBean changeUserMessageBean = new ChangeUserMessageBean();
                changeUserMessageBean.setIdCard(Objects.requireNonNull(binding.TIETIDCard.getText()).toString());
                changeUserMessageBean.setNickName(Objects.requireNonNull(binding.TIETNickName.getText()).toString());
                changeUserMessageBean.setPhonenumber(Objects.requireNonNull(binding.TIETTel.getText()).toString());
                changeUserMessageBean.setSex(binding.RBMan.isChecked() ? 0 : 1);
                int userId = MyApplication.getSharedPreferences().getInt("userId", 0);

                new Thread(() -> {
                    int rows = userdao.updateUserInfo(changeUserMessageBean.getNickName(), changeUserMessageBean.getPhonenumber(), changeUserMessageBean.getIdCard(), changeUserMessageBean.getSex(), userId);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (rows > 0) {
                                Toast.makeText(UserMessageActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                                UserMessageActivity.this.finish();
                            } else {
                                Toast.makeText(UserMessageActivity.this, "保存失败", Toast.LENGTH_SHORT).show();

                            }
                        }
                    });
                }).start();


            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}